﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace National.Dal
{
    public class DbContextFactory<TS> where TS : DbContext, new()
    {
        /// <summary>
        /// 唯一实例获取
        /// </summary>
        /// <returns></returns>
        public static DbContext GetCurrentDbContext()
        {
            //获取
            var dbContext = CallContext.GetData(typeof(TS).Name) as DbContext;
            if (dbContext != null)
                return dbContext;
            dbContext = new TS();
            //设置
            CallContext.SetData(typeof(TS).Name, dbContext);
            return dbContext;
        }
    }
}
